# -*- coding: utf-8 -*-

import pandas as pd
from .feature_repository import feature_repository

class feature_generator(object):
    def __init__(self, data_df=pd.DataFrame()):
        self.data_df = data_df
        self.feature_df = pd.DataFrame()
        self.label = pd.DataFrame()

    def feature_engineer(self):
        """
        用 self.data_df 生成 feature_df
        :return:
        """
        print("---------特征工程-----------")
        self.feature_df = pd.DataFrame()
        print(self.data_df.head(10))
        self.label = -self.data_df["成交价"].diff(1).fillna(0).to_frame()

        # 获取类feature_repository 的所有方法
        feature_repository_methods = [method_name for method_name in dir(feature_repository) if
                                    callable(getattr(feature_repository, method_name)) and not method_name.startswith("__")]
        cnt = 0

        for method_name in feature_repository_methods:
            print("正在执行生成特征 %s" % method_name)
            if len(method_name.split("_")) == 3:
                # 调用方法
                cnt +=1
                temp_series = getattr(feature_repository, method_name)(self.data_df)
                temp_series = temp_series.fillna(0)
                # 将方法结果添加到 feature_df 中
                self.feature_df[method_name] = temp_series

        print("已完成 %d 特征生成" %cnt)
        # self.feature_df = self.feature_df.drop(labels=["时间", "日期"], axis=1)
        return self.feature_df, self.label

